package code;

import java.util.ArrayList;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        ArrayList<user> list =new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");

            int input = sc.nextInt();
            switch(input) {
                case 1:
                    login(list);
                    System.out.println("是否退出");
                    System.out.println("1.是 / 否");
                    int input0 = sc.nextInt();
                    if (input0 == 1) {
                        System.out.println("成功退出");
                        return;
                    }else {
                        continue;
                    }
                case 2:
                    register(list);
                    continue;
                case 3:
                    forgetpwd(list);
                    continue;
                case 4:
                    System.out.println("成功退出");
                    return;
            }

        }

    }
    //登录方法
    private static void login(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = sc.next();
        boolean flag = isthere(list,userName);
        if (!flag) {
            System.out.println("用户" + userName + "没有注册");
            return;
        }

        System.out.println("请输入密码：");
        String pwd = sc.next();

        user userinfo = new user(userName,pwd);
        //判断用户名和密码是否正确
        boolean result = checkuser(list,userinfo);
        if (result) {
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败，用户名或密码错误");
        }

    }

    //判断输入是否存在
    private static boolean isthere(ArrayList<user> list,String username){
        for (int i = 0; i < list.size(); i++) {
            user one = list.get(i);
            String rightname = one.getUserName();
            if (rightname.equals(username)) {
                return true;
            }
        }
        return false;
    }

    //判断用户名和密码
    private static boolean checkuser(ArrayList<user> list, user userinfo) {
        for (int i = 0; i < list.size(); i++) {
            user one = list.get(i);
            if (one.getUserName().equals(userinfo.getUserName()) && one.getPsw().equals(userinfo.getPsw())) {
                return true;
            }
        }
        return false;
    }

    //注册方法
    private static void register(ArrayList<user> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        while(true) {
            //注册用户名
            System.out.println("请输入要注册的用户名：");
            username = sc.next();

            boolean i = isthere(list,username);
            if (i) {
                System.out.println("用户" + username + "已存在");
            }else {
                System.out.println("成功注册用户" + username );
                break;
            }
        }

        //注册密码
        String pwd1;
        String pwd2;
        while (true) {
            System.out.println("请输入要注册的密码：");
            pwd1 = sc.next();
            System.out.println("请再次输入要注册的密码：");
            pwd2 = sc.next();

            if (pwd1.equals(pwd2)) {
                System.out.println("输入正确");
                break;
            }else{
                System.out.println("两次密码不一致");
                continue;
            }
        }

        //将注册信息储存进Arraylist
        user u = new user(username,pwd1);//while循环里面定义的变量，在外不能用
        list.add(u);
        System.out.println("注册成功");

    }


    //忘记密码方法
    public static void forgetpwd(ArrayList<user> list) {

        Scanner sc = new Scanner(System.in);

            int index;
            while (true) {
                System.out.println("请输入你的用户名：");
                String input = sc.next();
                boolean flag = isthere(list, input);
                if (flag == true) {
                    System.out.println("请输入你的新密码：");
                    index = findindex(list,input);
                    break;
                } else {
                    System.out.println("未找到你的用户名，请重新输入");
                    continue;
                }

            }

            String pwd1;
            String pwd2;
            while (true) {
                pwd1 = sc.next();
                System.out.println("再输一次你的新密码：");
                pwd2 = sc.next();
                if (pwd1.equals(pwd2)) {
                    System.out.println("两次密码一致，更改密码成功");
                    break;
                } else {
                    System.out.println("与上次密码输入不一致，请重新输入");
                    continue;
                }

            }
            user users = list.get(index);
            users.setPsw(pwd2);
        System.out.println("修改成功");

    }

    private static int findindex(ArrayList<user> list,String username) {

        for (int i = 0; i < list.size(); i++) {
           user users = list.get(i);
           if (users.getUserName().equals(username)) {
               return i;
           }
        }
        return -1;
    }

}
